const wx2my = require('../../wx2my');

//同时发送异步代码的次数
let ajaxTimes = 0;
export const request = (url, data, params, type) => {
  ajaxTimes++; //显示加载中的效果

  wx2my.showLoading({
    title: "加载中",
    mask: true
  }); //定义公共部分的url

  var baseUrl = "https://hippo.dg88888888.com/user_api/";
  var requestMethod = type ? type : 'POST';
  var requestConType = 'application/x-www-form-urlencoded';

  if (typeof url != 'string') {
    baseUrl = url;
    requestMethod = url.method;
    requestConType = url.conType;
  }

  return new Promise((resolve, reject) => {
    wx2my.request({ ...params,
      url: baseUrl + url,
      header: {
        'content-type': requestConType
      },
      data: data,
      method: requestMethod,
      success: result => {
        console.log(result);

        if (result.data.code == -5) {
          wx2my.removeStorageSync('token');
        }

        resolve(result);
      },
      fail: err => {
        reject(err);
      },
      complete: () => {
        ajaxTimes--;

        if (ajaxTimes === 0) {
          //关闭正在等待的图标
          wx2my.hideLoading();
        }
      }
    });
  });
};